package com.globant.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.globant.bean.JobBean;
import com.globant.tab.R;

public class JobAdapter extends BaseAdapter {

	private List<JobBean> jobs;

	public JobAdapter(List<JobBean> jobs) {
		this.jobs = jobs;
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return jobs.size();
	}

	@Override
	public Object getItem(int position) {
		return jobs.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View item = null;
		JobViewHolder holder = null;
		
		if(convertView == null) {
			item = new View(parent.getContext());
		} else {
			item = (View) convertView;
		}
		
        if (item == null || !(item.getTag() instanceof JobViewHolder)) {
            
            LayoutInflater mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            item = mInflater.inflate(R.layout.job_view, parent, false);
            
            holder = new JobViewHolder();
            populateView(holder, item, jobs.get(position));
            item.setTag(holder);
       }else{
           holder = (JobViewHolder) item.getTag();
       }

		return item;
	}

	private void populateView(JobViewHolder holder, View item, JobBean jobBean) {
		holder.jobTitle = (TextView) item.findViewById(R.id.jobTitle);
		holder.levelRequieried = (TextView) item.findViewById(R.id.levelRequieried);
		holder.earnMoney = (TextView) item.findViewById(R.id.earnMoney);
		holder.earnExperience = (TextView) item.findViewById(R.id.earnExperience);
		holder.percentaje = (TextView) item.findViewById(R.id.percentaje);
		holder.progressBar = (ProgressBar) item.findViewById(R.id.progressBar);
		holder.familyRequired = (TextView) item.findViewById(R.id.familyRequired);
		
		holder.jobTitle.setText(jobBean.getTitle());
		holder.levelRequieried.setText(String.valueOf(jobBean.getLevelRequieried()));
		holder.percentaje.setText(String.valueOf(jobBean.getPercentaje()).concat("%"));
		holder.progressBar.setProgress(jobBean.getPercentaje());
		holder.familyRequired.setText(String.valueOf(jobBean.getMembersRequeried()));
		
		String money =  (String) item.getContext().getResources().getText(R.string.money);
		money = money.concat( (String) item.getContext().getResources().getText(R.string.plus));
		money = money.concat(String.valueOf(jobBean.getEarnMoney()));
		holder.earnMoney.setText(money);
		
		String experience =  (String) item.getContext().getResources().getText(R.string.plus);
		experience =  experience.concat(String.valueOf(jobBean.getEarnExperience()));
		experience =  experience.concat((String) item.getContext().getResources().getText(R.string.experince));
		holder.earnExperience.setText(experience);
	}
	
	class JobViewHolder {
		public TextView jobTitle;
		public TextView levelRequieried;
		public TextView earnMoney;
		public TextView earnExperience;
		public TextView percentaje;
		public ProgressBar progressBar;
		public TextView familyRequired;
	}
}


